#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\pricingengines\bond\bondfunctions.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

using namespace Cephei::QL::Instruments;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
namespace Cephei { namespace QL { namespace Pricingengines { namespace Bond {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IBondFunctions
	public ref class CBondFunctions : 
            public System::MarshalByRefObject,
            public Cephei::QL::Pricingengines::Bond::IBondFunctions
	{
	protected: 
		boost::shared_ptr<QuantLib::BondFunctions>* _ppBondFunctions;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::BondFunctions>* _phBondFunctions;
#endif
		Object^ _BondFunctionsOwner;     // reference to object that manages the storage for this object
	internal:
        CBondFunctions (boost::shared_ptr<QuantLib::BondFunctions>& childNative, Object^ owner);
        CBondFunctions (QuantLib::BondFunctions& childNative, Object^ owner);
        CBondFunctions (CBondFunctions^ copy);
        CBondFunctions (System::Type^ t);
#ifdef STRUCT
        CBondFunctions (QuantLib::BondFunctions childNative);
#endif       
#ifdef HANDLE
		CBondFunctions (QuantLib::Handle<QuantLib::BondFunctions>& childNative, Object^ owner);
		CBondFunctions (QuantLib::Handle<QuantLib::BondFunctions> childNative);
#endif
		virtual ~CBondFunctions ();
		!CBondFunctions ();

	internal:
		QuantLib::BondFunctions& GetReference ();
		boost::shared_ptr<QuantLib::BondFunctions>& GetShared ();
		QuantLib::BondFunctions* GetPointer ();
        void SetBondFunctions (boost::shared_ptr<QuantLib::BondFunctions> native)
        {
            if (_ppBondFunctions != NULL)
                delete _ppBondFunctions;
            _ppBondFunctions = new boost::shared_ptr<QuantLib::BondFunctions> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::BondFunctions>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
		virtual Double AccruedAmount (Cephei::QL::Instruments::IBond^ bond, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double AtmRate (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<Double>^ cleanPrice) ;
		virtual Double BasisPointValue (Cephei::QL::Instruments::IBond^ bond, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double BasisPointValue (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::IInterestRate^ yield, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double Bps (Cephei::QL::Instruments::IBond^ bond, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double Bps (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::IInterestRate^ yield, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double Bps (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double CleanPrice (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::Termstructures::IYieldTermStructure^ discount, Double zSpread, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double CleanPrice (Cephei::QL::Instruments::IBond^ bond, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double CleanPrice (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::IInterestRate^ yield, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double CleanPrice (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double Convexity (Cephei::QL::Instruments::IBond^ bond, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double Convexity (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::IInterestRate^ yield, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double Duration (Cephei::QL::Instruments::IBond^ bond, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<QL::Cashflows::Duration::TypeEnum>^ type, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double Duration (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::IInterestRate^ yield, Microsoft::FSharp::Core::FSharpOption<QL::Cashflows::Duration::TypeEnum>^ type, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Boolean IsTradable (Cephei::QL::Instruments::IBond^ bond, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual DateTime MaturityDate (Cephei::QL::Instruments::IBond^ bond) ;
		virtual Double NextCashFlowAmount (Cephei::QL::Instruments::IBond^ bond, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refDate) ;
		virtual DateTime NextCashFlowDate (Cephei::QL::Instruments::IBond^ bond, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refDate) ;
		virtual Double NextCouponRate (Cephei::QL::Instruments::IBond^ bond, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double PreviousCashFlowAmount (Cephei::QL::Instruments::IBond^ bond, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refDate) ;
		virtual DateTime PreviousCashFlowDate (Cephei::QL::Instruments::IBond^ bond, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refDate) ;
		virtual Double PreviousCouponRate (Cephei::QL::Instruments::IBond^ bond, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual DateTime StartDate (Cephei::QL::Instruments::IBond^ bond) ;
		virtual Double Yield (Cephei::QL::Instruments::IBond^ bond, Double cleanPrice, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxIterations, Microsoft::FSharp::Core::FSharpOption<Double>^ guess) ;
		virtual Double YieldValueBasisPoint (Cephei::QL::Instruments::IBond^ bond, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double YieldValueBasisPoint (Cephei::QL::Instruments::IBond^ bond, Cephei::QL::IInterestRate^ yield, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double ZSpread (Cephei::QL::Instruments::IBond^ bond, Double cleanPrice, Cephei::QL::Termstructures::IYieldTermStructure^ prm1, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxIterations, Microsoft::FSharp::Core::FSharpOption<Double>^ guess) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CBondFunctions_Factory : public System::MarshalByRefObject,  public IBondFunctions_Factory
	{
	public:
    };
   
/*Cephei*/ } /*QL*/ } /*Pricingengines*/ } /*Bond */}
